<!DOCTYPE HTML>
<html>
	<head>
		<title>上传头像</title>
	</head>
	<style>
		body {
		}
		img {
			display: block;
			width: 100px;
			height: 100px;
			margin: center;
		}
		.portrait {
			display: inline-block;
			position: relative;
			overflow: hidden;
			margin: 10px;
		}
		
		.portrait input.upload {
			position: absolute;
			top: 0;
			right: 0;
			margin: 0;
			padding: 0;
			font-size: 20px;
			cursor: pointer;
			opacity: 0;
			filter: alpha(opacity=0);
		}
	</style>
	<body>
		<h2>编辑头像</h2>
		<?php $info = array('id'=>234, 'name'=>'王五'); ?>
		<p>用户名：<?php echo $info['name'] ?></p>
		<p>现有头像：</p>
		<div class="div1">
		<div class="portrait"><img src="<?php echo 'img/'.$info['id'].'.png?rand='.rand(); ?>" onerror="this.src='img/default.png'" /></div>
		<form action="" method="post" enctype="multipart/form-data">
			上传头像：<input name="pic" type="file" class="upload" /><br>
			<input type="submit" value="保存头像" />
		</form>
		</div>
		<?php 
			echo '<pre>';
			print_r($_FILES);
			echo '</pre>';
			if(!empty($_FILES['pic']))
			{
				$pic_info = $_FILES['pic'];
				if($pic_info['error'] > 0)
				{
					$error_msg = '上传错误';
					switch ($pic_info['error']) {
						case 1:
							$error_msg .= '文件大小超过php.ini中的限制值';
							break;
						case 2:
							$error_msg .= '文件大小超过表单中max_file_size选项指定的值！';
							break;
						case 3:
							$error_msg .= '文件只有部分被上传！';
							break;
						case 4:
							$error_msg .= '没有文件被上传！';
							break;
						case 5:
							$error_msg .= '找不到临时文件！';
							break;
						case 6:
							$error_msg .= '找不到临时文件夹！！';
							break;
						case 7:
							$error_msg .= '文件写入失败！';
							break;
						default:
							$error_msg .= '未知错误！';				
							break;
					}
					echo $error_msg;
					return FALSE;
				}
				//获取上传文件的类型
//				$type = substr(strrchr($pic_info['name'], '.'), 1);
				$type = $_FILES['pic']['type'];
				//
				$allow_type = array('image/jpeg', 'image/png', 'image/gif');
				if(!in_array($type, $allow_type)) {
					echo '图像类型不符合要求，请选择jpeg、png、gif格式图片';
					return false;
				}
				//
				$img_info = getimagesize($pic_info['tmp_name']);
				$width = $img_info[0];
				$height = $img_info[1];
				list($width, $height) = getimagesize($pic_info['tmp_name']);
				$maxwidth = 90;
				$maxheight = 90;
				//
				if($width > $height) {
					$newwidth = $maxwidth;
					$newheight = round($newheight * $height/$width);
				} else {
					$newheight = $maxheight;
					$newwidth = round($newheight * $width/$height);
				}
				//绘制画布
				echo $width;
				echo $newheight;
				$thumb = imagecreatetruecolor($newwidth, $newheight);
				//
				$source = imagecreatefrompng($pic_info['tmp_name']);
				//
				imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
				 
				//命名图片文件,图片从临时目录保存到img目录
				$filename = 'img/'.$info['id'].'.png';
				imagepng($thumb, $filename, 9);
				if(!move_uploaded_file($pic_info['tmp_name'], $filename)) {
					echo '头像上传失败！';
					return false;
				}
				echo '$_FILES["tmp_name"]=>'.$pic_info['tmp_name'];
			}
		?>
	</body>
</html>

